package algebra.query;

import option.QueryKeywords;
import utils.exception.SyntaxParsingException;
import utils.parser.Creator;
import utils.parser.Parser;
import kb.DescriptionLogicsKB;
import entities.concept.Concept;
import algebra.op.PrimaryOP;
import algebra.op.ProjectionOP;
import algebra.op.SelectionOP;
import algebra.query.pd.ProjDesc;

/**
 * A special query (C, Pd)
 * 
 * @author j55wu
 * 
 */
public class ObjectQuery {

	private final Concept c;
	private final ProjDesc pd;

	public static ObjectQuery getObjectQuery(String cs, String pds, DescriptionLogicsKB kb) {
		try {
			Concept c = Parser.conParser(
					Parser.getListOfTokensFromTokenizer(cs), kb);
			ProjDesc pd = Creator
					.createProjDesc(Parser
							.getListOfTokensFromTokenizer(pds), kb);
			return new ObjectQuery(c, pd);
		} catch (SyntaxParsingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Concept getC() {
		return this.c;
	}

	public ProjDesc getPd() {
		return this.pd;
	}

	public ObjectQuery(Concept c, ProjDesc pd) {
		this.c = c;
		this.pd = pd;
	}

	// public Query getSelectionCondition(){
	// return this.sub;
	// }

	// translate into an algebraic expression
	public ProjectionOP translateAsExpression(DescriptionLogicsKB kb) {
		ProjectionOP pq = new ProjectionOP(pd, new SelectionOP(
				new PrimaryOP(kb), c, kb), kb);
		return pq;
	}

	// @Override
	// public boolean isPure() {
	// return true;
	// }

	@Override
	public String toString() {
		return "(" + " " + this.c.toString() + ", [" + this.pd.toString()
				+ "] )";
	}

	public String toSerializedString() {
		return " (" + QueryKeywords.QINFORM + " " + this.c.toSerializedString()
				+ " " + this.pd.toSerializedString() + ") ";
	}

}
